#!/usr/bin/perl -w

package Purl::DB;

use lib qw(/home/don/purl/lib);
use strict;
our $objects;

sub init {
    my $class  = shift;
    my $dbType = shift;
    $dbType = 3 unless $dbType;
    my @dbTypes = ( "NULL", "XML", "SQL", "Flatfile" );
    if ($dbType == 1) {
       require Purl::DB::XML;
       Purl::DB::XML->import(qw(load));
       print "Imported XML DB.\n";
    } elsif ($dbType == 2) {
       require Purl::DB::SQL;
       Purl::DB::SQL->import(qw(load));
    } else {
       require Purl::DB::Flatfile;
       Purl::DB::Flatfile->import(qw(load));
    }
    #eval { require "Purl::DB::" . $dbTypes[$dbType]; };
    $objects = load();
    unless ($objects) {
        print "Database is invalid. Please repair.\n";
        exit 0;
    }
    unless ( verify($objects) ) {
        print "Database did not validate. Please repair.\n";
        exit 0;
    }
    bless $objects, $class;
    return $objects;
}

sub getAttr {
    my $self  = shift;
    print "\$self is a " . ref($self) . "\n";
    my $dbref = shift;
    my $attr  = shift;
    print "getAttr call - \$self has $#{$self} indices.\n";
    return "#-1" unless ( defined ${$self}[$dbref] );
    return "#-1" unless ( exists ${$self}[$dbref]{$attr} );
    return ${$self}[$dbref]{$attr};
}

sub verify {
   return 1;
}

1;
